#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion


namespace GameFramework.ScreenSystem
{
    /// <summary>
    /// The pause menu comes up over the top of the game,
    /// giving the player options to resume or quit.
    /// </summary>
    class PauseMenuScreen : MenuScreen
    {
        #region Fields

        private string menuMusicToPlay;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public PauseMenuScreen(string musicToPlay,MenuTransition Transition):base(musicToPlay,Transition)
        {
            MenuEntries.Add("Resume Game");
            MenuEntries.Add("Quit Game");

            // Flag that there is no need for the game to transition
            // off when the pause menu is on top of it.
            IsPopup = true;

            // Ensure the specified music cue name to play isn't empty or null.
            if (!String.IsNullOrEmpty(musicToPlay))
            {
                menuMusicToPlay = musicToPlay;
                GameManager.AudioManager.Play(menuMusicToPlay);
            }
        }

        #endregion

        #region Handle Input


        /// <summary>
        /// Responds to user menu selections.
        /// </summary>
        protected override void OnSelectEntry(int entryIndex)
        {
            // Check to make sure that the pause menu music is playing
            if (!GameManager.AudioManager[menuMusicToPlay].IsPlaying)
            {
                GameManager.AudioManager.StopAllPlaying();
                GameManager.AudioManager.Play(menuMusicToPlay);
            }

            switch (entryIndex)
            {
                case 0:
                    // Stop the pause menu music from playing.
                    GameManager.AudioManager.StopAllPlaying();
                    GameManager.AudioManager.Play("PlayScreenMusic");

                    // Resume the game.
                    ExitScreen();
                    
                    break;

                case 1:
                    // Quit the game, after a confirmation message box.
                    const string message = "Are you sure you want to quit this game?";

                    // Create a message box screen and use the music audio cue "MainMenuMusic" 
                    GameManager.AudioManager.StopAllPlaying();
                    MessageBoxScreen messageBox = new MessageBoxScreen(message, "QuitMessageBoxMusic" );

                    messageBox.Accepted += QuitMessageBoxAccepted;

                    ScreenManager.AddScreen(messageBox);
                    break;
            }
        }


        /// <summary>
        /// When the user cancels the pause menu, resume the game.
        /// </summary>
        protected override void OnCancel()
        {
            // Stop all currently playing audio cues
            GameManager.AudioManager.StopAllPlaying();

            ExitScreen();
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to quit" message box. This uses the loading screen to
        /// transition from the game back to the main menu screen.
        /// </summary>
        void QuitMessageBoxAccepted(object sender, EventArgs e)
        {
            // Use the music audio cue "MainMusicMusic" 
            LoadingScreen.Load(ScreenManager, LoadMainMenuScreen, false, "MainMenuMusic");
        }


        /// <summary>
        /// Loading screen callback for activating the main menu screen,
        /// used when quitting from the game.
        /// </summary>
        void LoadMainMenuScreen(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new BackgroundScreen());
            ScreenManager.AddScreen(new MainMenuScreen("MainMenuMusic",MenuTransition.Transition4));
        }

        #endregion

        #region Draw


        /// <summary>
        /// Draws the pause menu screen. This darkens down the gameplay screen
        /// that is underneath us, and then chains to the base MenuScreen.Draw.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.FadeBackBufferToBlack(TransitionAlpha * 2 / 3);

            base.Draw(gameTime);
        }


        #endregion
    }
}
